<?php
require 'utils.php';
header("Content-Type: application/json");
$res = [];
// CPU信息
$res['cpu_info']['core_count'] = (int)exec('cat /proc/cpuinfo | grep "core id" | sort -n | uniq | wc -l');
$res['cpu_info']['thread_count'] = (int)exec('cat /proc/cpuinfo | grep processor | wc -l');
$res['cpu_info']['name'] = exec('cat /proc/cpuinfo | grep name | uniq | awk -F \': \' \'{print $2}\'');
exec('cat /proc/cpuinfo | grep MHz | awk \'{print $4}\'',$output);
$res['cpu_info']['frequency'] = json_decode('['.implode(',',$output).']');
unset($output);
//硬盘信息
exec("lsblk -Jb",$output);
$t = json_decode(implode('',$output),true);
$res['disk_info'] = [];
for ($i=0; $i < count($t['blockdevices']); $i++) {
    $res['disk_info'][]= [
        'device'=>'/dev/'.$t['blockdevices'][$i]['name'],
        'partitions'=>getDiskInfo("/dev/{$t['blockdevices'][$i]['name']}")
    ];
}

//内存信息
$res['memory_info'] = json_decode(exec('free -b | grep Mem | awk \'{ printf("{\"total\":%s,\"used\":%s,\"free\":%s,\"shared\":%s,\"buff_cache\":%s,\"available\":%s}", $2, $3, $4, $5, $6,$7) }\''));
$res['swap_info'] = json_decode(exec('free -b | grep Swap | awk \'{ printf("{\"total\":%s,\"used\":%s,\"free\":%s}", $2, $3, $4) }\''));

$res['net_info'] = SysInfo::getNetworkInterfacesInfo();
$res['run_time'] = (int)exec('cat /proc/uptime | awk \'{print $1}\'');
echo json_encode(makeRes(1,$res));